<template>
  <div class="user-list">
    <div class="list-header">
      <h3>{{ title }}</h3>
    </div>
    
    <div class="list-items">
      <div 
        v-for="user in users" 
        :key="user.id" 
        class="user-item"
        :class="{ 'active': isActive(user) }"
        @click="selectUser(user)"
      >
        <div class="user-avatar">
          {{ user.username.charAt(0).toUpperCase() }}
        </div>
        <div class="user-info">
          <div class="user-name">{{ user.username }}</div>
          <div class="user-status" v-if="user.online">在线</div>
          <div class="user-status offline" v-else>离线</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue';
import { useChatStore } from '../store/chat';
import { computed } from 'vue';

const props = defineProps({
  users: {
    type: Array,
    default: () => []
  },
  title: {
    type: String,
    default: '用户列表'
  }
});

const emit = defineEmits(['select-user']);
const chatStore = useChatStore();

const currentContact = computed(() => chatStore.contact);

// 判断用户是否是当前选中的联系人
const isActive = (user) => {
  return currentContact.value && currentContact.value.id === user.id;
};

// 选择用户
const selectUser = (user) => {
  chatStore.setContact(user);
  emit('select-user', user);
};
</script>

<style scoped>
.user-list {
  display: flex;
  flex-direction: column;
  height: 100%;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  overflow: hidden;
}

.list-header {
  padding: 10px 15px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #e0e0e0;
}

.list-header h3 {
  margin: 0;
  font-size: 16px;
}

.list-items {
  flex: 1;
  overflow-y: auto;
}

.user-item {
  display: flex;
  align-items: center;
  padding: 10px 15px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.user-item:hover {
  background-color: #f5f5f5;
}

.user-item.active {
  background-color: #e3f2fd;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #4caf50;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  margin-right: 10px;
}

.user-info {
  flex: 1;
}

.user-name {
  font-weight: 500;
  margin-bottom: 4px;
}

.user-status {
  font-size: 12px;
  color: #4caf50;
}

.user-status.offline {
  color: #9e9e9e;
}
</style>